package com.yanjiali.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yanjiali.entity.SettingContent;
import com.yanjiali.entity.SettingContentUser;
import com.yanjiali.enumerate.SettingTypeEnum;
import com.yanjiali.model.R;
import com.yanjiali.model.resp.GetSettingJsonResp;
import com.yanjiali.service.SettingContentService;
import com.yanjiali.service.SettingContentUserService;
import com.yanjiali.vo.AdvancedSettingVO;
import com.yanjiali.vo.AllSettingVO;
import com.yanjiali.vo.BasicsSettingVO;
import com.yanjiali.vo.SettingContentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yanjiali.mapper.SettingMapper;
import com.yanjiali.entity.Setting;
import com.yanjiali.service.SettingService;
/**
* @Package: com.yanjiali.service.impl
* @Author: yanjiali
* @Created: 2025/4/30 15:01
*/
@Service
public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> implements SettingService{

    @Autowired
    private SettingContentService settingContentService;

    @Autowired
    private SettingContentUserService settingContentUserService;

    @Override
    public R<GetSettingJsonResp> getSettingJson() {
        //TODO 虽然知道对应设置id，但是还是从数据库中获取对应一级
        Long basicsSettingId = 1L, advancedSettingId = 2L;
        BasicsSettingVO basicsSettingVO = new BasicsSettingVO();
        AdvancedSettingVO advancedSettingVO = new AdvancedSettingVO();

        LambdaQueryWrapper<Setting> alqw = new LambdaQueryWrapper<>();
        alqw.eq(Setting::getSetName, SettingTypeEnum.ADVANCED_SETTING.getSettingName());

        Setting setting = getOne(alqw);
        if(setting != null) {
            advancedSettingId = setting.getId();
            advancedSettingVO.setName(setting.getSetName());
        }

        LambdaQueryWrapper<Setting> blqw = new LambdaQueryWrapper<>();
        blqw.eq(Setting::getSetName, SettingTypeEnum.BASICS_SETTING.getSettingName());
        setting = getOne(blqw);

        if(setting != null) {
            basicsSettingId = setting.getId();
            basicsSettingVO.setName(setting.getSetName());
        }


        //获取基础设置集合
        basicsSettingVO.setSettingContentListVO(getBasicsSetting(basicsSettingId));

        //获取高级设置集合
        advancedSettingVO.setSettingContentListVO(getAdvancedSetting(advancedSettingId));


        return R.ok(new GetSettingJsonResp(new AllSettingVO(basicsSettingVO, advancedSettingVO)));
    }

    /**
     * 获取高级设置内容集合
     * @return
     */
    private List<SettingContentVO> getAdvancedSetting(Long advancedSettingId) {
        LambdaQueryWrapper<SettingContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SettingContent::getSetId, advancedSettingId);
        List<SettingContent> settingContentList = settingContentService.list(lambdaQueryWrapper);

        if(settingContentList != null) {
            List<SettingContentVO> list = settingContentList.stream().map(settingContent -> {
                SettingContentVO settingContentVO = new SettingContentVO();

                settingContentVO.setId(settingContent.getId());
                settingContentVO.setBriefIntroduction(settingContent.getBriefIntroduction());
                settingContentVO.setName(settingContent.getName());
                settingContentVO.setSetId(settingContent.getSetId());

                //TODO 获取对应用户的设置状态
                boolean status = settingContentUserService.getUserSettingContentStatus(settingContent.getId());
                settingContentVO.setStatus(status);

                return settingContentVO;
            }).collect(Collectors.toList());

            return list;
        }

        return new ArrayList<>();
    }

    /**
     * 获取基础设置内容集合
     * @return
     */
    private List<SettingContentVO> getBasicsSetting(Long basicsSettingId) {
        LambdaQueryWrapper<SettingContent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SettingContent::getSetId, basicsSettingId);
        List<SettingContent> settingContentList = settingContentService.list(lambdaQueryWrapper);

        if(settingContentList != null) {
            List<SettingContentVO> list = settingContentList.stream().map(settingContent -> {
                SettingContentVO settingContentVO = new SettingContentVO();

                settingContentVO.setId(settingContent.getId());
                settingContentVO.setBriefIntroduction(settingContent.getBriefIntroduction());
                settingContentVO.setName(settingContent.getName());
                settingContentVO.setSetId(settingContent.getSetId());

                //TODO 获取对应用户的设置状态
                boolean status = settingContentUserService.getUserSettingContentStatus(settingContent.getId());
                settingContentVO.setStatus(status);

                return settingContentVO;
            }).collect(Collectors.toList());

            return list;
        }

        return new ArrayList<>();
    }
}
